package com.drodds.game;

import java.util.ArrayList;
import android.content.Context;
import android.util.DisplayMetrics;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.view.View;

public class UIWidgetDock {
	/*
	 * Right of dock when in landscape
	 */
	public static final float absLandscapeX = 0.15f;
	/*
	 * Top of dock when in portrait
	 */
	public static final float absPortraitY = 0.90f;
	
	private ArrayList<Rect> slots = new ArrayList<Rect>();
	public static final int LANDSCAPE = Configuration.ORIENTATION_LANDSCAPE;
	public static final int PORTRAIT = Configuration.ORIENTATION_PORTRAIT;
	public int orientation;
	
	private Context context;
	private ArrayList<View> buttons;
	private Rect dockBounds;
	
	public void init(Context ctxt) {
		buttons = new ArrayList<View>();
		context = ctxt;
		dockBounds = new Rect();
		updatePos();
	}
	
	public void push(View btn) {
		buttons.ensureCapacity(buttons.size() + 1);
		buttons.add(btn);
		CalcSlots();
	}
	
	public void add(int index, View btn) {
		buttons.ensureCapacity(buttons.size() + 1);
		buttons.add(index, btn);
		CalcSlots();
	}
	
	public void remove(int pos) {
		if (buttons.size() >= pos) 
			return;
		else
			buttons.remove(pos);
		
		CalcSlots();
	}
	
	private void CalcSlots() {
		while (slots.size() != buttons.size()) {
			if (slots.size() < buttons.size()) {
				slots.add(new Rect());
			}
			else if (slots.size() > buttons.size()) {
				slots.remove(0);
			}
		}
		
		for (int i = 0; i < slots.size(); i++) {
			if (orientation == PORTRAIT) {
				slots.get(i).top = dockBounds.top;
				slots.get(i).bottom = dockBounds.bottom;
				slots.get(i).left = i * dockBounds.width()/slots.size();
				slots.get(i).right = i * dockBounds.width()/slots.size() + dockBounds.width()/slots.size() - 1;
			}
			else if (orientation == LANDSCAPE) {
				slots.get(i).top = i * dockBounds.height() / slots.size();
				slots.get(i).bottom = i * dockBounds.height()/slots.size() + dockBounds.height()/slots.size() - 1;
				slots.get(i).left = dockBounds.left;
				slots.get(i).right = dockBounds.right;
			}
		}
	}
	
	public View get(int pos) {
		if (buttons.size() >= pos)
			return null;
		else
			return buttons.get(pos);
	}
	
	public Rect getViewBounds(View v) {
		Rect r = new Rect();
		if (buttons.contains(v)) {
			r = slots.get(buttons.indexOf(v));
		}
		return r;
	}
	
	public Rect getBounds() {
		return dockBounds;
	}
	
	public void updatePos() {
		orientation = context.getResources().getConfiguration().orientation;
		
		DisplayMetrics dims = context.getResources().getDisplayMetrics();
		dockBounds.left = 0;
		dockBounds.bottom = dims.heightPixels;
		
		if (orientation == PORTRAIT) {
			dockBounds.top = Math.round(dims.heightPixels * absPortraitY);
			dockBounds.right = dims.widthPixels;
		}
		else {
			dockBounds.top = 0;
			dockBounds.right = Math.round(dims.widthPixels * absLandscapeX);
		}
		
		CalcSlots();
	}
}
